/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.bionic.shsc.dao.implementations;


import com.bionic.shsc.dao.interfaces.IQuery;
import com.bionic.shsc.db.DB;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.NamingException;

/**
 *
 * @author Ann
 */
public class MySQLQuery<ResultObject> implements IQuery {

    private Connection connection;

    public MySQLQuery() throws SQLException,  NamingException {
       connection = DB.getInstance().getConnection();
    }
    
    public Connection getConnection (){
    return connection;
    }

    @Override
    public boolean executeInsertStatement(String statementText) {
        try {
            Statement statement = connection.createStatement();
            int updateQuery = 0;
            updateQuery = statement.executeUpdate(statementText);
            return (updateQuery != 0);
        } catch (SQLException ex) {
            Logger.getLogger(MySQLQuery.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
    }

    @Override
    public boolean executeUpdateStatement(String statementText) {
        try {
            Statement statement = connection.createStatement();
            int updateQuery = 0;
            updateQuery = statement.executeUpdate(statementText);
            return (updateQuery != 0);
        } catch (SQLException ex) {
            Logger.getLogger(MySQLQuery.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
    }

    @Override
    public boolean executeDeleteStatement(String statementText) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public int getRSRecordCount(ResultSet rs) throws SQLException {
        int res = 0;
        if (rs != null) {
            while (rs.next()) {
                res++;
            }
        }
        return res;
    }

    @Override
    public ResultSet getRSForQuery(String queryText) {
        Statement statement = null;
        ResultSet rs = null;
        try {
            boolean c = connection.isClosed();
            statement = connection.createStatement();
            rs = statement.executeQuery(queryText);
            return rs;
        } catch (SQLException e) {
            String error = e.getMessage();
            // LOG ERRORS or raise!!!!!!!!!!!!!!!
            //GlobalLogger.LogEvent(e.getMessage(), EventType.SqlError);
            return null;
        }
    }

   
}
